package com.sunb2b.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;

public class SearchEngine1 {
    public static IndexWriter indexWriter;
    public static IndexReader indexReader;
    private static FSDirectory diskDirectory;
    private static IndexWriterConfig iwConfig;
    
    public static void init() throws IOException{
        iwConfig = new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer()); 
        diskDirectory = SimpleFSDirectory.open(new File("c:/bb/index"));
        indexWriter = new IndexWriter(diskDirectory, iwConfig);
        Document blankDoc = new Document();
        Field f = new Field("region", false, "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO);
        blankDoc.add(f);
        indexWriter.addDocument(blankDoc);
        indexWriter.commit();
        indexReader = IndexReader.open(diskDirectory);
    }
    
    public static void t1() throws IOException{
        Document doc = new Document();
        Field f = new Field("region", false, "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO);
        BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream("f:/workspace/b2b/src/com/sunb2b/test/region.txt")));
        String line = r.readLine();
        int k = 0;
        while(line!=null){
            f.setValue(line);
            doc.add(f);
            indexWriter.addDocument(doc);
            line = r.readLine();
            System.out.println((k++) +" : "+ line);
        }
        indexWriter.commit();
    }
    
    public static Document[] search(String queryString, int start, int total) throws ParseException, IOException{
        IndexSearcher searcher = new IndexSearcher(indexReader);
        Query query = IKQueryParser.parse("region", queryString);
        //Query query = parser.parse(queryString);
        //query = parser.Term(queryString);
        int size = start + total;
        TopScoreDocCollector res = TopScoreDocCollector.create(size, false);
        searcher.search(query, res);
        System.out.println("Total hits: " + res.getTotalHits());
        TopDocs tds = res.topDocs(start, total);
        ScoreDoc[] sd = tds.scoreDocs;
        Document [] docs = new Document[sd.length];
        for (int i = 0; i < sd.length; i++) {
            docs[i] = indexReader.document(sd[i].doc);
        }
        return docs;
    }
    
    public static void main(String[] args) throws IOException, ParseException {
        init();
        //t1();
        System.out.println("index was created----------------------------");
        BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream("f:/workspace/b2b/src/com/sunb2b/test/key.txt")));
        String line = r.readLine();
        System.out.println(line);
        Document [] docs = SearchEngine1.search(line, 0, 10);
        for(Document doc : docs){
            System.out.println(doc.getValues("region")[0]);
        }
    }

}
